package top.jandmla.shop.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.jandmla.shop.bean.Discount;
import top.jandmla.shop.bean.pojo.QueryDto;
import top.jandmla.shop.service.DiscountService;
import top.jandmla.shop.utils.result.BaseResult;

/**
 * <p>
 *
 * </p>
 */
@RestController
@RequestMapping("admin/discount/")
public class DiscountController {

    @Autowired
    private DiscountService service;

    @PostMapping("test")
    public String test(){
        return "discount test is ok!";
    }

    @PostMapping("insert")
    public BaseResult<String> insertDiscount(@RequestBody Discount discount){
         return service.save(discount) ? BaseResult.success("添加成功！")
                 : BaseResult.fail(500,"添加失败！");
    }

    @PostMapping("delete")
    public BaseResult<String> deleteDiscount(@RequestBody Discount discount){
        return service.removeById(discount.getId()) ? BaseResult.success("删除成功！")
                : BaseResult.fail(500,"删除失败！");
    }

    @PostMapping("selectPage")
    public BaseResult<IPage<Discount>> selectPage(@RequestBody QueryDto<Discount> dto){
        return service.selectPage(dto);
    }

    @PostMapping("update")
    public BaseResult<String> updateDiscount(@RequestBody Discount discount){
        return service.updateById(discount) ? BaseResult.success("修改成功！")
                : BaseResult.fail(500,"修改失败！");
    }

}
